package com.xcity.game.activity2.rechargereward;

import java.util.Collection;

import com.xcity.Server;
import com.xcity.db.entity.Activity;
import com.xcity.db.entity.Player;
import com.xcity.game.activity2.ActivityIds;
import com.xcity.game.activity2.ActivityImpl;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.event.GameEvents;
import com.xcity.game.player.PlayerService;
import com.xcity.game.recharge.RechargeTemplate;
import com.xcity.pomelo.PomeloRoute;
import com.xcity.pomelo.push.msg.ActivityStatePush;
import com.xcity.pomelo.push.msg.RechargeRewardPush;
import com.xcity.pomelo.request.RechargeRewardGetRewardRequest;
import com.xcity.pomelo.request.RechargeRewardListRequest;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.RechargeRewardGetRewardResponse;
import com.xcity.pomelo.response.RechargeRewardListResponse;

import naga.x.App;
import naga.x.event.Event;
import naga.x.event.EventListener;
import naga.x.net.Session;
import naga.x.net.annotation.OPHandler;
import naga.x.net.impl.pomelo.annotation.PomeloOP;
import naga.x.service.ServiceException;

/**
 * 充值送
 * @author shipeng.zhu
 */
@OPHandler
public class RechargeRewardActivity extends ActivityImpl implements EventListener {

	public static int[] REWARD_ITEMS = new int[]{80230, 80229};
//	public static int[] NEED_RECHARGE = new int[]{100, 100};
	public static int[] RECHARGE_TEMP_ID = new int[]{30015, 30016};
	
	public static int STATE_NEED_RECHARGE = 0;	//未充值
	public static int STATE_CAN_GET = 1;		//已充值可领
	public static int STATE_GOT = 2;			//已领
	
	public RechargeRewardActivity(Activity entity) {
		super(entity);
		App.getApp().getEventManager().register(this);
	}

	@Override
	public String getId() {
		return getClass().getName();
	}

	@Override
	public int[] getEventTypes() {
		return new int[]{
				GameEvents.EVENT_RECHARGE,
				GameEvents.EVENT_PLAYER_LOADED,
		};
	}

	@Override
	public void handleEvent(Event event) {
		switch (event.getType()) {
		case GameEvents.EVENT_RECHARGE:
			handleRecharge((Player)event.getParameter(0), (int)event.getParameter(1), (RechargeTemplate)event.getParameter(2));
			break;
		case GameEvents.EVENT_PLAYER_LOADED:
			playerLoaded((Player)event.getParameter(0));
			break;
		default:
			break;
		}
	}

	@Override
	public void init() {
	}

	@Override
	public void save() {
	}

	@Override
	public void startup() throws ServiceException {
		onStart();
	}

	@Override
	public void shutdown() throws ServiceException {
		onEnd();
	}

	@Override
	public boolean update() {
		return true;
	}

	@Override
	public void setDirty() {
		// TODO Auto-generated method stub

	}
	
	public void playerLoaded(Player player){
		fixNotGotAndPush(player);
//		playerNotify(player);
	}
	
	public void fixNotGotAndPush(Player player){
		boolean needSave = false;
		int stateData = player.getPool().getInt(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, 0);
		for(int i = 0; i < REWARD_ITEMS.length; i++){
			int got = (stateData >> (i << 1)) & 0x3;
			if(got == STATE_GOT){//0未充值不可领 1充值可领 2已领
				got = STATE_NEED_RECHARGE;
				stateData = ~(~stateData | (0x3 << (i << 1))) | (got << (i << 1));
				needSave = true;
			}
		}
		if(needSave){
			player.poolSet(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, stateData);
		}
		player.send(new ActivityStatePush(ActivityIds.ACTIVITY_RECHARGE_REWARD, Activity.STATE_OPENED));
	}
	
	@Deprecated
	public void playerNotify(Player player){
		boolean show = false;
		int stateData = player.getPool().getInt(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, 0);
		for(int i = 0; i < REWARD_ITEMS.length; i++){
			int got = (stateData >> (i << 1)) & 0x3;
			if(got != STATE_GOT){//0未充值不可领 1充值可领 2已领
				show = true;
				break;
			}
		}
		if(show){
			player.send(new ActivityStatePush(ActivityIds.ACTIVITY_RECHARGE_REWARD, Activity.STATE_OPENED));
		}
	}
	
	public void handleRecharge(Player player, int money, RechargeTemplate rechargeTemplate){
//		int total = player.getPool().getInt(PlayerPool.PROPERTY_RECHARGE_REWARD_TOTAL_RECHARGE, 0);
//		total += money;
//		player.poolSet(PlayerPool.PROPERTY_RECHARGE_REWARD_TOTAL_RECHARGE, total);
		
		int stateData = player.getPool().getInt(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, 0);
		for(int i = 0; i < REWARD_ITEMS.length; i++){
			if(rechargeTemplate.getId() == RECHARGE_TEMP_ID[i]){
				int got = (stateData >> (i << 1)) & 0x3;
				if(got == STATE_NEED_RECHARGE){//0未充值不可领 1充值可领 2已领
					got = STATE_CAN_GET;
					stateData = ~(~stateData | (0x3 << (i << 1))) | (got << (i << 1));
					player.getPool().put(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, stateData);
					if(player.isOnline()){
						//TODO
						player.send(new RechargeRewardPush(i, got));
					}
				}
			}
		}
	}
	public void onStart(){
		Collection<Player> players = App.getApp().getServiceManager().get(PlayerService.class).getOnlinePlayers();
		for (Player player : players) {
//			playerNotify(player);
			fixNotGotAndPush(player);
		}
	}
	
	public void onEnd(){
		Server.broadcast(new ActivityStatePush(ActivityIds.ACTIVITY_RECHARGE_REWARD, Activity.STATE_CLOSED));
	}
	
	@PomeloOP(PomeloRoute.ACTIVITY_RECHARGE_REWARD_GET)
	protected void handleGetReward(Session session, RechargeRewardGetRewardRequest req){
		Player player = session.getClient();
		int type = req.getType();
		if(type < 0 || type >= RECHARGE_TEMP_ID.length){
			player.send(new HintResponse(req.getSerial(), MessageConst.ACTIVITY_RECHARGE_REWARD_NO_TYPE));
			return;
		}

		int stateData = player.getPool().getInt(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, 0);
		int got = (stateData >> (type << 1)) & 0x3;
		if(got == STATE_NEED_RECHARGE){
			player.send(new HintResponse(req.getSerial(), MessageConst.ACTIVITY_RECHARGE_REWARD_NO_RECHARGE));
			return;
		}else if(got == STATE_GOT){
			player.send(new HintResponse(req.getSerial(), MessageConst.ACTIVITY_RECHARGE_HAS_GOT));
			return;
		}
		player.getBag().addItemWithoutException(REWARD_ITEMS[type], 1, Cause.RECHARGE_REWARD_ACTIVITY);
		got = STATE_NEED_RECHARGE;//STATE_GOT;
		stateData = ~(~stateData | (0x3 << (type << 1))) | (got << (type << 1));
		player.poolSet(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, stateData);
		player.send(new RechargeRewardGetRewardResponse(req.getSerial(), type, got, REWARD_ITEMS[type]));
		boolean needPush = true;
		for(int i = 0, size = REWARD_ITEMS.length; i < size; i++){
			int state = (stateData >> (i << 1)) & 0x3;
			if(state != STATE_GOT){
				needPush = false;
				break;
			}
		}
		if(needPush){
			player.send(new ActivityStatePush(ActivityIds.ACTIVITY_RECHARGE_REWARD, Activity.STATE_CLOSED));
		}
		LOG.info("[RECHARGEREWARD_GET]ID[{}]TYPE[{}]STATE[{}]DATA[{}]");
	}

	@PomeloOP(PomeloRoute.ACTIVITY_RECHARGE_LIST)
	protected void handleGetReward(Session session, RechargeRewardListRequest req){
		Player player = session.getClient();
		int stateData = player.getPool().getInt(PlayerPool.PROPERTY_RECHARGE_REWARD_STATE, 0);
		player.send(new RechargeRewardListResponse(req.getSerial(), stateData, REWARD_ITEMS, RECHARGE_TEMP_ID));
	}
}
